package com.dj.uphome.web.controller.system;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.dj.common.web.controller.CoreCrudRestController;
import com.dj.common.web.vo.EasyuiPage;
import com.dj.uphome.web.service.system.SysRoleUserService;
import com.dj.uphome.web.service.system.SysUserService;
import com.dj.uphome.web.vo.system.SysRoleUserVo;
import com.dj.uphome.web.vo.system.SysRoleVo;
import com.dj.uphome.web.vo.system.SysUserVo;


/** 
 * 类名称：用户角色管理
 */
@Controller
@RequestMapping(value = "/role/user")
public class RoleUserController extends CoreCrudRestController{
	@Autowired
	private SysRoleUserService roleUserService;

	
	
	@RequestMapping(value = "/listAuthorize", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ResponseEntity<List<SysRoleVo>> listAuthorize(@RequestParam int userId) throws Exception {
		return new ResponseEntity<>(roleUserService.listAllForAuthorize(userId),HttpStatus.OK);
	}
	/**
	 * 保存角色菜单权限
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST,produces = { MediaType.APPLICATION_JSON_VALUE })
	public  ResponseEntity<SysRoleUserVo> save(@RequestParam int userId, @RequestParam String roleIds) throws Exception {
		if (!StringUtils.isEmpty(roleIds)) {
			String[] roleIds_ = roleIds.split(",");
			int[] roleIds_int = new int[roleIds_.length];
			for(int i=0;i<roleIds_.length;i++){
				roleIds_int[i]=Integer.parseInt(roleIds_[i]);
			}
			roleUserService.create(userId, roleIds_int);
		}
		return new ResponseEntity<>(HttpStatus.OK);
	}

}
